using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace MessageBoardControl
{
	/// <summary>
	/// Summary description for GroupList.
	/// </summary>
	[DefaultProperty("Text"), 
		ToolboxData("<{0}:ThreadList runat=server></{0}:ThreadList>")]
	public class ThreadList : System.Web.UI.WebControls.WebControl
	{
		private string text;
	
		[Bindable(true), 
			Category("Appearance"), 
			DefaultValue("")] 
		public string Text 
		{
			get
			{
				return text;
			}

			set
			{
				text = value;
			}
		}

		private string url;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string URL 
		{
			get
			{
				return url;
			}

			set
			{
				url = value;
			}
		}

		private int forumId;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public int ForumId 
		{
			get
			{
				return forumId;
			}

			set
			{
				forumId = value;
			}
		}

		private MessageBoard.User currentUser;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.User CurrentUser 
		{
			get
			{
				return currentUser;
			}

			set
			{
				currentUser = value;
			}
		}

		private string imagesFolder;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string ImagesFolder 
		{
			get
			{
				return imagesFolder;
			}

			set
			{
				imagesFolder = value;
			}
		}

		private MessageBoard.Language language;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Language Language 
		{
			get
			{
				return language;
			}

			set
			{
				language = value;
			}
		}

		/// <summary> 
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		protected override void Render(HtmlTextWriter output)
		{
			CurrentUser.MakeRequest();
		
			output.WriteBeginTag("table");
			output.WriteAttribute("width", "100%");
			output.WriteAttribute("cellpadding", "0");
			output.WriteAttribute("cellspacing", "0");
			output.RenderBeginTag(HtmlTextWriterTag.Tr);
			output.WriteBeginTag("td");
			output.WriteAttribute("class", "Forum_TableBorder");
			output.Write(HtmlTextWriter.TagRightChar);

			output.WriteBeginTag("table");
			output.WriteAttribute("width", "100%");
			output.WriteAttribute("cellspacing", "1");
			output.WriteAttribute("cellpadding", "2");
				output.RenderBeginTag(HtmlTextWriterTag.Tr);

					output.WriteBeginTag("td");
					output.WriteAttribute("colspan", "3");
					output.WriteAttribute("class", "Forum_Title1");
					output.Write(HtmlTextWriter.TagRightChar);
							output.Write(Translate("Title"));
					output.WriteEndTag("td");

					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title1");
					output.WriteAttribute("width", "100");
					output.WriteAttribute("align", "center");
					output.Write(HtmlTextWriter.TagRightChar);
						output.Write(Translate("Author"));
					output.WriteEndTag("td");
				
					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title1");
					output.WriteAttribute("width", "80");
					output.WriteAttribute("align", "center");
					output.Write(HtmlTextWriter.TagRightChar);
						output.Write(Translate("Posts"));
					output.WriteEndTag("td");

					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title1");
					output.WriteAttribute("width", "80");
					output.WriteAttribute("align", "center");
					output.Write(HtmlTextWriter.TagRightChar);
						output.Write(Translate("Views"));
					output.WriteEndTag("td");

					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title1");
					output.WriteAttribute("width", "150");
					output.WriteAttribute("align", "center");
					output.Write(HtmlTextWriter.TagRightChar);
						output.Write(Translate("Last Post"));
					output.WriteEndTag("td");

				output.RenderEndTag();
				int i=0;
				foreach(MessageBoard.Thread thread in MessageBoard.Thread.GetAll(MessageBoard.Forum.Get(ForumId)))
				{
					i++;
					output.RenderBeginTag(HtmlTextWriterTag.Tr);

						output.WriteBeginTag("td");
						output.WriteAttribute("class", "Forum_Title4");
						output.WriteAttribute("width", "20");
						output.WriteAttribute("align", "center");
						output.Write(HtmlTextWriter.TagRightChar);
							if(thread.Locked==true)
							{
								output.Write("<img src="+imagesFolder+"/yellockfolder.gif border=0 title = '"+Translate("A Closed thread - no new replies accepted")+"'>");								
							}
							else
							{
								if(thread.TotalPosts>10)
								{
									output.Write("<img src="+imagesFolder+"/hotclosed.gif border=0 title = '"+Translate("More than 10 replies")+"'>");
								}
								else
								{
									output.Write("<img src="+imagesFolder+"/closed.gif border=0 title = '"+Translate("An Open Thread")+"'>");
								}
							}
						output.WriteEndTag("td");

						output.WriteBeginTag("td");
						output.WriteAttribute("width", "20");
						output.WriteAttribute("align", "center");
						output.WriteAttribute("class", "Forum_Title3");
						output.Write(HtmlTextWriter.TagRightChar);
							output.Write("<img src="+imagesFolder+"/icon"+thread.Icon+".gif border=0>");
						output.WriteEndTag("td");

						output.WriteBeginTag("td");
						output.WriteAttribute("class", "Forum_Title4");
						output.Write(HtmlTextWriter.TagRightChar);
							output.WriteBeginTag("a");
							output.WriteAttribute("href", URL+"Forum=PostList&ThreadId="+thread.Id);
							output.WriteAttribute("class", "Forum_Link");
							output.Write(HtmlTextWriter.TagRightChar);
								output.Write(thread.Subject);
							output.WriteEndTag("a");
						output.WriteEndTag("td");

						output.WriteBeginTag("td");
						output.WriteAttribute("class", "Forum_Title3");
						output.WriteAttribute("align", "center");
						output.Write(HtmlTextWriter.TagRightChar);
							output.Write(thread.FirstPost.User.DisplayName);
						output.WriteEndTag("td");

						output.WriteBeginTag("td");
						output.WriteAttribute("class", "Forum_Title4");
						output.WriteAttribute("align", "center");
						output.Write(HtmlTextWriter.TagRightChar);
							output.Write(thread.TotalPosts);
						output.WriteEndTag("td");

						output.WriteBeginTag("td");
						output.WriteAttribute("class", "Forum_Title3");
						output.WriteAttribute("align", "center");
						output.Write(HtmlTextWriter.TagRightChar);
							output.Write(thread.TotalViews);
						output.WriteEndTag("td");

						output.WriteBeginTag("td");
						output.WriteAttribute("class", "Forum_Title4");
						output.WriteAttribute("align", "center");
						output.Write(HtmlTextWriter.TagRightChar);
							output.Write(thread.LatestPost.PostDate.ToString(), "{0:dd MMM yyyy h:m tt}");
							output.Write("<br>"+Translate("by")+" ");
							output.Write(thread.LatestPost.User.DisplayName);
						output.WriteEndTag("td");


					output.RenderEndTag();
				}
				if(i==0)
				{
					output.RenderBeginTag(HtmlTextWriterTag.Tr);

						output.WriteBeginTag("td");
						output.WriteAttribute("colspan", "7");
						output.WriteAttribute("class", "Forum_Title3");
						output.Write(HtmlTextWriter.TagRightChar);
							output.Write(Translate("No threads found in this forum."));
						output.WriteEndTag("td");

					output.RenderEndTag();
				}
				output.RenderBeginTag(HtmlTextWriterTag.Tr);

					output.WriteBeginTag("td");
					output.WriteAttribute("colspan", "7");
					output.WriteAttribute("class", "Forum_Title1");
					output.Write(HtmlTextWriter.TagRightChar);
					output.Write("&nbsp;");
					output.WriteEndTag("td");

				output.RenderEndTag();

			output.WriteEndTag("table");

			output.WriteEndTag("td");
			output.RenderEndTag();
			output.WriteEndTag("table");
		}

		private MessageBoard.CustomControl customControl = MessageBoard.CustomControl.Get("ThreadList");

		private string Translate(string word)
		{
			return MessageBoard.Translation.Get(customControl, word, Language).Equal;
		}
	}
}
